package com.zhenyi.tuangou.core;

import java.sql.SQLException;
import java.util.Date;

import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.zhenyi.common.model.TgGroup;
import com.zhenyi.common.model.TgGroupMember;
import com.zhenyi.common.model.TgGroupProduct;
import com.zhenyi.common.model.TgOrder;

public class OrderUtils {
	/**
	 * 团长已经支付，开团成功
	 */
	protected final int PAY_YES = 1;
	/**
	 * 团长没有支付
	 */
	protected final int PAY_NO = 0;

	public static final String GROUP_PRODUCT = "GROUP_PRODUCT:";
	public static final String GROUP = "GROUP:";
	public static final String SETTLEMENT = "SETTLEMENT:";//购物车结算

	public void processOrder(TgOrder order) {
		String extra = order.getExtra();
		System.out.println("extra:" + extra);
		if (StrKit.isBlank(extra)) {
			return;
		}
		if (extra.contains(GROUP_PRODUCT)) {
			// 处理开团
			Long groupProductId = Long.parseLong(extra.replace(GROUP_PRODUCT, ""));
			System.out.println("groupProductId: " + groupProductId);
			boolean ISOK = leaderGroup(groupProductId, order.getId());// 发起团购成功
			System.out.println(ISOK);
		} else if (extra.contains(GROUP)) {
			// 处理参团
			Long id = Long.parseLong(extra.replace(GROUP, ""));
			System.out.println(id);
			Boolean ISOK = joinGroup(id, order.getId());// 参团成功
			System.out.println(ISOK);
		}else if(extra.contains(SETTLEMENT)){
			//购物车结算
			order.setStatus(PAY_YES);
			order.update();
		}
	}

	/**
	 * 随机开团者宣传语
	 * 
	 * @return
	 */
	protected String getLeaderWord() {
		return "ascascd";
	}

	/**
	 * 发起团购
	 * 
	 * @param num
	 * @param groupId
	 * @param productId
	 * @return
	 */
	protected boolean leaderGroup(long groupProductId, long orderId) {
		boolean flag = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {
				TgGroupProduct tgGroupProduct = TgGroupProduct.dao.findById(groupProductId);
				Integer num = tgGroupProduct.getNum();

				TgGroup tgGroup = new TgGroup();
				tgGroup.setLeaderSay(getLeaderWord());// 开团者宣言
				// tgGroup.setJoinLeft(num - 1);// 剩余座位
				tgGroup.setStatus(TgGroup.STATUS_PAID);//
				tgGroup.setGroupProductId(groupProductId);
				tgGroup.save();

				TgOrder order = TgOrder.dao.findById(orderId);
				order.setStatus(PAY_YES);// 已支付
				order.update();

				TgGroupMember tgGroupMember = new TgGroupMember();// 团购人员信息
				tgGroupMember.setUserId(order.getUserId());
				tgGroupMember.setOrderId(orderId);
				tgGroupMember.setGroupId(tgGroup.getId());
				tgGroupMember.setAddTime(new Date());
				tgGroupMember.setIsLeader(1);
				tgGroupMember.save();

				tgGroup.updateJoinLeft();
				tgGroup.update();
				return true;
			}
		});
		return flag;
	}

	/***
	 * 我要参团
	 * 
	 * @param orderId
	 * @param grouptId
	 * @return
	 */
	protected Boolean joinGroup(long groupBuyId, long orderId) {
		boolean flag = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {
				TgGroup tgGroup = TgGroup.dao.findById(groupBuyId);
				int num = tgGroup.getJoinLeft() + 1;
				if (tgGroup.getStatus().equals(TgGroup.STATUS_CLOSE)
						|| tgGroup.getStatus().equals(TgGroup.STATUS_SUCCESS)) {
					LogKit.error("团购已经关闭或者团购已经完成");
					return false;
				} else {
					if (num == 0) {
						tgGroup.setStatus(TgGroup.STATUS_SUCCESS);
						LogKit.error("团购已经完成");
						return false;
					} else if (num > 0) {
						// tgGroup.setJoinLeft(num - 1);// 剩余座位
						TgGroupProduct tgGroupProduct = TgGroupProduct.dao.findById(tgGroup.getGroupProductId());
						TgOrder order = TgOrder.dao.findById(orderId);
						order.setStatus(PAY_YES);// 已支付
						order.update();
						TgGroupMember tgGroupMember = new TgGroupMember();// 团购人员信息
						tgGroupMember.setUserId(order.getUserId());
						tgGroupMember.setOrderId(orderId);
						tgGroupMember.setGroupId(groupBuyId);
						tgGroupMember.setAddTime(new Date());
						tgGroupMember.setIsLeader(0);
						tgGroupMember.save();

						tgGroup.updateJoinLeft();
						tgGroup.update();
						return true;
					}
					return false;
				}
			}
		});
		return flag;
	}

	/**
	 * 向第三方查询并更新订单状态
	 * 
	 * @param order
	 */
	public TgOrder queryOrder(TgOrder order) {
		return order;
	}
}
